// pages/pickeOrder/pickeOrder.j

import dataParser from '../../utils/dataParser'
import Dialog from '@vant/weapp/dialog/dialog';
import backPareser from '../../utils/backPareser'
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pickOrders:[
      {
        "id": 14,
        "orderid": "ORD2025030517284577411p7l",
        "servicename": "刷碗服务",
        "serviceprice": 50,
        "picurl": "http://192.168.101.65:9000/photos/1/servicepic/9fa08a579a3d1f9729a2372b6df5fc5e.png",
        "serviceid": 18,
        "acceptid": "o_rGL7fUm0Uqo_r39ynuYA32F2tE",
        "acceptname": null,
        "createat": "2025-03-05T09:28:46.000+00:00",
        "accepttel": null,
        "status": -1,
        "specification": "刷碗服务/深度清洗（较多餐具）/50",
        "address": null,
        "updateat": "2025-03-05T09:28:46.000+00:00"
      },
      {
        "id": 15,
        "orderid": "ORD202503051741509341rz83",
        "servicename": "厨房清洁服务",
        "serviceprice": 150,
        "picurl": "http://192.168.101.65:9000/photos/1/servicepic/4d88c4838b42968a93b643c015a031f1.png",
        "serviceid": 28,
        "acceptid": "o_rGL7fUm0Uqo_r39ynuYA32F2tE",
        "acceptname": null,
        "createat": "2025-03-05T09:41:51.000+00:00",
        "accepttel": null,
        "status": -1,
        "specification": "厨房清洁服务/全面清洁（包括地面、冰箱等）/150",
        "address": null,
        "updateat": "2025-03-05T09:41:51.000+00:00"
      }
    ],
    finish:[],
    cookie:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    
    console.log(222);
    wx.getStorage({
      key:'sessionId',
      success:(res)=>{
        console.log(res.data);
        this.setData({
          cookie:`JSESSIONID=${res.data}`
        })
        console.log(this.data.cookie);
      }
    })
    this.getData()
  },

  getData() {
  console.log(this.data.cookie);

  wx.request({
    url: 'http://localhost:9001/wx/getMyPickOrder',
    method: 'GET',
    withCredentials: true,
    header: {
      // 从本地缓存中获取 sessionId，并添加到请求头中
      'cookie': `JSESSIONID=${wx.getStorageSync('sessionId')}`
    },
    success: (res) => {
      // 处理返回的数据
    this.setData({
      pickOrders:res.data.data
    })

    const parseOreders=dataParser.parseSericeSpecifications(this.data.pickOrders)
      console.log(parseOreders);
      this.setData({
        pickOrders: parseOreders
      });
  
      // 如果有新的 cookie，更新本地缓存
      if (res.header && res.header['Set-Cookie']) {
        const newSessionId = res.header['Set-Cookie'].match(/JSESSIONID=([^;]+)/)[1];
        if (newSessionId) {
          wx.setStorageSync('sessionId', newSessionId); // 更新本地缓存中的 sessionId
        }
      }
    },
    fail: (err) => {
      console.error('请求失败', err);
    }
  });
    
  },


  finishOrder(event){
    Dialog.confirm({
      title: '提示',
      message: '确认完成订单',
    })
      .then(() => {

      

       

        wx.getStorage({
          key:'sessionId',
          success:(res1)=>{
            const cookie = `JSESSIONID=${res1.data}`; // 确保 cookie 格式正确
            this.setData({ cookie });
           
            this.setData({
              finish:event.currentTarget.dataset.index
            })
            const spec=this.data.finish.specification.join('/')
            console.log(spec);

            wx.request({
              url: 'http://localhost:9001/wx/finishOrder',
              method:'POST',
              withCredentials: true, 
              header: {
                'cookie': cookie
              },
              data:{
           "id": this.data.finish.id,
           "orderid": this.data.finish.orderid, 
           "servicename": this.data.finish.servicename,
            "price": this.data.finish.price, 
            "picurl": this.data.finish.picurl, 
            "serviceid": this.data.finish.serviceid, 
            "acceptid": this.data.finish.acceptid, 
            "acceptname": this.data.finish.acceptname, 
            "createat": this.data.finish.createat, 
            "accepttel": this.data.finish.accepttel, 
            "status":this.data.finish.status, 
            "specification": spec,
             "address": this.data.finish.address, 
             "updateat": this.data.finish.updateat,
             "startservicetime":this.data.finish.startservicetime ,
             "endservicetime": this.data.finish.endservicetime 
              },
              success:(res)=>{

                Toast('已完成订单');
                this.getData();
    

              }

            })















         



          }})

      
      })
      .catch(() => {
        // on cancel
      });
   


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})